/* This file holds all the function that gets called from the XUL that will use
 an xpcom component */


/* Every time a page is focused, handleFocus is called */
function callHandleFocus(pageID, pageURL, pageContent){
	var logger = Components.classes['@peercafe.org/pcLogger;1']
					.createInstance(Components.interfaces.pcILogger);
	try {
		if (pageContent.length > 1000) {
			pageContent = pageContent.slice(0,1000);
		}
			
		// local copy, since the xpcom can't access it directly
		//logger.pcLog("PAGE CONTENT in callHandleFocus: " + pageContent + "\n")
		
		var prefManager = Components.classes["@mozilla.org/preferences-service;1"]
						.getService(Components.interfaces.nsIPrefBranch);
		var logging = prefManager.getBoolPref("extensions.peercafe.logging");
		
		if(logging && !isBlackListed(pageURL)){
			var timerComponent = Components.classes['@peercafe.org/pcPageTimer;1']
		                                    .createInstance(Components.interfaces.pcIPageTimer);
			// the pagecontent is needed for the (eventual) clustering
	        timerComponent.handleFocus(pageID, pageURL, pageContent);
		} else {
			logger.pcLog("found a blacklisted page, not going to log it: -> " + pageURL)
		}
		
	} catch (anError) {
		logger.pcLog("An exception occurred in callHandleFocus: " + anError)
	}
}

/* When firefox gets closed, handleLastFocus is called */
function callHandleLastFocus(){
	var logger = Components.classes['@peercafe.org/pcLogger;1']
					.createInstance(Components.interfaces.pcILogger);
	try {
		var timerComponent = Components.classes['@peercafe.org/pcPageTimer;1']
		                                    .createInstance(Components.interfaces.pcIPageTimer);
	    timerComponent.handleLastFocus();
	}catch(e){
		logger.pcLog("An exception occurred in callHandleLastFocus: " + anError)
	}
}

// populate the list in the peercafe info panel
function populatePagesInfo(){
	var logger = Components.classes['@peercafe.org/pcLogger;1']
					.createInstance(Components.interfaces.pcILogger);
	try{
		
		eraseTreeContent("pageInfoList");
		
		var timerComponent = Components.classes['@peercafe.org/pcPageTimer;1']
	                                    .createInstance(Components.interfaces.pcIPageTimer);
		var infoString = timerComponent.infoToString() // xpcom function
		
		if(infoString == ""){
			return;
		}
		
		// else populate
		var pages = infoString.split(".NEXT.");
		
		var infoList = document.getElementById("pageInfoList");
		
		for (var i = 0; i < pages.length - 1; i++) {
			var pieces = pages[i].split(".ITEM.");

			var newItem = document.createElement("treeitem");
			var newRow = document.createElement("treerow");
			
			newItem.appendChild(newRow); // new row
			
			// id that will be used while clustering
			appendRowChild(newRow, parseInt(pieces[6]))
			// url of the page
			appendRowChild(newRow, pieces[1])
			// time spent on that page
			appendRowChild(newRow, pieces[2])
			// average seconds per visit
			var timestamps = pieces[5]
			var timeStampsArray = timestamps.split('.TIMEITEM.')
			appendRowChild(newRow, Math.ceil(parseInt(pieces[2]) / (timeStampsArray.length - 1)))
			// was the page printed?
			appendRowChild(newRow, pieces[3])
			// was the page bookmarked?
			appendRowChild(newRow, pieces[4])
			
			// cluster
			
			/*
			const cid = "@peercafe.org/XPCOMSample/PeerCafe;1";
		    obj = Components.classes[cid].createInstance();
		    obj = obj.QueryInterface(Components.interfaces.IPeerCafe);
			
			//appendRowChild(newRow, obj.PageInCluster(pieces[6] + "")
			
			*/
			
			infoList.appendChild(newItem);
	    }
		
	}catch(e){
		logger.pcLog("An exception occurred in populatePagesInfo: " + e)
	}
}

/*Every time firefox gets closed, the information about the pages must be stored,
thus serialize() is called*/
function callPageInfoSerialize(){
	var logger = Components.classes['@peercafe.org/pcLogger;1']
					.createInstance(Components.interfaces.pcILogger);
					
	try{
		var timerComponent = Components.classes['@peercafe.org/pcPageTimer;1']
	                                    .createInstance(Components.interfaces.pcIPageTimer);
		timerComponent.serialize()
	}catch(e){
		logger.pcLog("An exception occurred in callPageInfoSerialize: " + e)
	}
	
}

/*Every time firefox gets opened, the information about the pages must be restored,
thus deserialize() is called*/
function callDeserialize(){
	var logger = Components.classes['@peercafe.org/pcLogger;1']
					.createInstance(Components.interfaces.pcILogger);
					
	try{
		var timerComponent = Components.classes['@peercafe.org/pcPageTimer;1']
	                                    .createInstance(Components.interfaces.pcIPageTimer);
		timerComponent.deserialize()
	}catch(e){
		logger.pcLog("An exception occurred in callDeserialize: " + e)
	}	
}

function callTrend(){
	var logger = Components.classes['@peercafe.org/pcLogger;1']
					.createInstance(Components.interfaces.pcILogger);
					
	try{
		var timerComponent = Components.classes['@peercafe.org/pcPageTimer;1']
	                                    .createInstance(Components.interfaces.pcIPageTimer);
		timerComponent.trend()
	}catch(e){
		logger.pcLog("An exception occurred in callTrend(): " + e)
	}
}